﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace PlugInTest
{
    public partial class Form1 : Form
    {
        private MyPlugIn.CustomPlugIn PlugIn;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Kuge.DataExchange.PlugIn.Result Ret;

            PlugIn = new MyPlugIn.CustomPlugIn();
            PlugIn.Happended += PlugIn_Happended;

            Ret = PlugIn.Login();
            if((Ret != null) &&(Ret.Succeed != true))
            {
                this.Close();
            }

            //try
            //{
            //    string AliasFile = Path.Combine(Application.StartupPath, "Alias.dat");
            //    if (File.Exists(AliasFile))
            //    {
            //        XmlDocument xml = new XmlDocument();
            //        xml.Load(AliasFile);
            //        foreach (XmlNode node in xml.DocumentElement.ChildNodes)
            //        {
            //            XmlNode Including = node.SelectSingleNode("@Including");
            //            if (Including.InnerText == "True")
            //            {
            //                //if (Columns.ContainsKey(node.Name))
            //                {
            //                    Console.WriteLine("Columns.Add(\"{0}\",\"{1}\");", node.Name, node.InnerText);
            //                }
            //            }
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    //ShowBlockTips(true, "Loading Alias.dat", ex.ToString());
            //}

        }

        private void PlugIn_Happended(string Command, bool isError, object arg3)
        {
            Console.WriteLine("{0}:{1}", Command, isError);
        }

        private void btnSearchBarcode_Click(object sender, EventArgs e)
        {

        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            #region 模拟BTS6000/BTS7000产生测试数据

            Kuge.DataExchange.PlugIn.Result Ret;
            List<Kuge.DataExchange.PlugIn.DataItem> ItemList = new List<Kuge.DataExchange.PlugIn.DataItem>();

            try
            {
                string AliasFile = Path.Combine(Application.StartupPath, "Alias.dat");
                if (File.Exists(AliasFile))
                {
                    Random R = new Random();
                    XmlDocument xml = new XmlDocument();
                    xml.Load(AliasFile);
                    foreach (XmlNode node in xml.DocumentElement.ChildNodes)
                    {
                        XmlNode Including = node.SelectSingleNode("@Including");
                        if (Including.InnerText == "True")
                        {
                            ItemList.Add(new Kuge.DataExchange.PlugIn.DataItem(node.Name, node.InnerText, (10 * R.NextDouble()).ToString("F5"), "", "", "P"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //ShowBlockTips(true, "Loading Alias.dat", ex.ToString());
            }

            Ret = PlugIn.Push("UPLOAD_DATA", true, ItemList, txtBarcode.Text, "");
            if ((Ret != null) && (Ret.Succeed == true))
            {
                MessageBox.Show(Ret.Message, "上传成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(Ret.Message, "上传失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            #endregion
        }
    }
}
